﻿#include "FITKCCXAdaptorPart.h"
#include "FITKCalculixINPReader.h"
#include "InpKeyLineSpliter.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceModel/FITKUnstructuredMesh.h"
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"


namespace IO
{

    QString FITKCCXAdaptorPart::getAdaptorClass()
    {
        return "IO::FITKCCXAdaptorModel";
    }

    bool FITKCCXAdaptorPart::adaptR()
    {
        AbaqusData::FITKAbaqusPart* part = dynamic_cast<AbaqusData::FITKAbaqusPart*>(_dataObj);
        if (_reader == nullptr || part == nullptr) return false;
 
       _reader->backLine();
       QString line = _reader->readLine();

       InpKeyLineSpliter keySpliter(line, _reader);
       const QString sheader = keySpliter.getHeader().toLower();

       if (sheader == "node" )
       {
           bool ok = this->readNode(part, keySpliter["nset"]);
           if (!ok) return false;
       }
       else if (sheader == "element")
       {
           bool ok = this->readElement(part, keySpliter["elset"], keySpliter["type"]);
           if (!ok) return false;
       }

       return true;
    }

    bool FITKCCXAdaptorPart::adaptW()
    {
        return false;
    }

   

    bool FITKCCXAdaptorPart::readNode(AbaqusData::FITKAbaqusPart* part, const QString& setName)
    {
        if (part == nullptr) return false;
        auto mesh = part->getMesh();
        QList <int> NodeList;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            if(line.startsWith("*")) break;

            QStringList sList = line.split(",");
            Q_ASSERT(sList.size() == 4);

             
            // 文件中点的编号
            int pointID = sList[0].toInt();
            NodeList.append(pointID);
            // 节点坐标
            double x = sList[1].toDouble();
            double y = sList[2].toDouble();
            double z = sList[3].toDouble();
            mesh->addNode(pointID, x, y, z);
            _reader->sendCurrentPercent();
        }
        //不需要创建集合
        if (setName.isEmpty()) return true;
        //创建集合
        Interface::FITKComponentManager* compMgr = part->getComponentManager();
        if (compMgr == nullptr) return false;
        Interface::FITKModelSet* set = new Interface::FITKModelSet(Interface::FITKModelEnum::FITKModelSetType::FMSNode);
        set->setDataObjectName(setName);
        set->setModel(part);
        set->setAbsoluteMember(NodeList);
        compMgr->appendDataObj(set);
        return true;
    }


    bool FITKCCXAdaptorPart::readElement(AbaqusData::FITKAbaqusPart* part, const QString& setName, const QString &eleTypeS)
    {
        if (part == nullptr) return false;
        auto mesh = part->getMesh();
        
        //确定解析的单元类型
        Core::FITKEnumTransfer<Interface::FITKAbaMeshPartVTK::AbaEleType> abaEleTypeTrafer;
        bool isValid = false;
        auto eleType = abaEleTypeTrafer.fromString(eleTypeS, isValid);
        if (!isValid)
        {
            _reader->consoleMessage(3, QObject::tr("UnSupport element type: %1").arg(eleTypeS));
            return false;
        }
        QList <int> eleIDs;

        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            if (line.startsWith("*")) break;

            //单独处理一个单元的数据 支持多行的点
            while (line.trimmed().back() == QChar(',')/* || !_reader->atEnd()*/)
            {
                QString tempLine = _reader->readLine();
                if (tempLine.startsWith("*"))
                {
                    _reader->backLine();
                    break;
                }
                line += tempLine;
            }
            QStringList sList = line.split(',');
            if (sList.size() < 1)
            {
                _reader->consoleMessage(3, QObject::tr("readElement error,element line count : %1") + sList.size());
                return false;
            }
            QList<int> sIDList;
            // 文件中单元的编号
            int eleID = sList[0].toInt();
            eleIDs.append(eleID);
            for (int i = 1; i < sList.size(); i++)
            {
                // 文件中单元的编号
                if (sList[i].isEmpty()) continue;
                int nodeID = sList[i].toInt();
                sIDList.append(nodeID);
            }
            if (!mesh->appendElement(eleType, eleID, sIDList))
            {
                _reader->consoleMessage(3, QObject::tr("ReadElement Error,Element NodeIDList Count Error, ElementID : %1").arg(eleID));
                return false;
            }
            _reader->sendCurrentPercent();
        }
        //不需要创建集合
        if (setName.isEmpty()) return true;
        //创建集合
        Interface::FITKComponentManager* compMgr = part->getComponentManager();
        if (compMgr == nullptr) return false;
        Interface::FITKModelSet* set = new Interface::FITKModelSet(Interface::FITKModelEnum::FITKModelSetType::FMSElem);
        set->setDataObjectName(setName);
        set->setModel(part);
        set->setAbsoluteMember(eleIDs);
        compMgr->appendDataObj(set);
        return true;
    }

}


